//
//  PopverAnimator.swift
//  PersonnalDiary
//
//  Created by 徐宽阔 on 15/9/29.
//  Copyright © 2015年 XDKOO. All rights reserved.
//

import UIKit

let XDKPopverAnimatorDidShow = "XDKPopverAnimatorDidShow"
let XDKPopverAnimatorDidDisMiss = "XDKPopverAnimatorDidDisMiss"

class PopverAnimator: UIPresentationController, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning
{
    var PopverFrame:CGRect = CGRectZero
    // 判断是否已经打开谈框
    var isOpend = false
    
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let po = PopverPresentationController(presentedViewController: presented , presentingViewController: presenting)
        
        // 设置frame
        po.PopverFrame = PopverFrame
        
        return po
    }
    
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isOpend = true
        
        NSNotificationCenter.defaultCenter().postNotificationName(XDKPopverAnimatorDidShow, object: self)
        
        return self
    }
    
    
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isOpend = false
        NSNotificationCenter.defaultCenter().postNotificationName(XDKPopverAnimatorDidDisMiss, object: self)
        return self
    }
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval
    {
        return 0.5
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning)
    {
        // UITransitionContextToViewControllerKey
        // UITransitionContextFromViewControllerKey
        // UITransitionContextFromViewKey, and UITransitionContextToViewKey
        
        if isOpend
        {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
            
            transitionContext.containerView()?.addSubview(toView)
            
            toView.transform = CGAffineTransformMakeScale(1.0, 0.0);
            toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                toView.transform = CGAffineTransformIdentity
                }) { (_) -> Void in
                    transitionContext.completeTransition(true)
            }
        }else{
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
                fromView.transform = CGAffineTransformMakeScale(1.0,0.000001)
                }, completion: { (_) -> Void in
                    transitionContext.completeTransition(true)
            })
            
        }
        
    }
}
